package Application.UIEditApplication.view.ui
{
	import Application.UIEditApplication.def.Def;
	import Application.UIEditApplication.def.Manager;
	import Application.UIEditApplication.view.ui.cell.ComponentListCell;
	import Application.UIEditApplication.view.ui.cell.ComponentPropertyCell;
	
	import XComponent.XComponents;
	import XComponent.hide.XEvent;
	import XComponent.show.XFrame;
	import XComponent.show.XLoader;
	import XComponent.show.XPopWnd;
	
	import com.utils.keymanager.Key;
	
	import def.XG;
	
	import fl.controls.Button;
	import fl.controls.ComboBox;
	import fl.controls.DataGrid;
	import fl.controls.List;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.core.UIComponent;
	import fl.data.DataProvider;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	public class ComponentPropertyMed 	extends UIBaseMed
	{
		static public const NAME : String = "ComponentPropertyMed";
		static public const COMPONENTPROPERTYCHANGED : String = "COMPONENTPROPERTYCHANGED";
		
		private var m_popWnd : XPopWnd;
		
		private var m_dgComponentProperty : DataGrid;
		
		private var m_dpComponentProperty : DataProvider;
		
		private var m_comPropertyLst : Array;
		private var m_id:String;
		private var m_component:*;
		
		public function ComponentPropertyMed()
		{
			m_popWnd = new XPopWnd;
			this.viewComponent = m_popWnd;
			
			super(NAME,[Def.STAGE_ALIGN_MIDDLE]);
		}
		
		override public function onRegister():void
		{
			m_dgComponentProperty = new DataGrid;
			m_dpComponentProperty = new DataProvider;
			
			m_dgComponentProperty.columns=["Property","value"];
			m_dgComponentProperty.y = 28;
			m_dgComponentProperty.x = 5;
			
			var column : DataGridColumn = m_dgComponentProperty.columns[0];
			column.cellRenderer = ComponentPropertyCell;
			column.setWidth(100);
			
			column = m_dgComponentProperty.columns[1];
			column.cellRenderer = ComponentPropertyCell;
			column.setWidth(100);
			
			m_dgComponentProperty.dataProvider = m_dpComponentProperty;
			m_dgComponentProperty.width = 200;
			
			m_dgComponentProperty.addEventListener(XEvent.XEVENT_CHANGED,OnPropertyChanged);
			
			m_dgComponentProperty.addEventListener(KeyboardEvent.KEY_DOWN,OnKeyDown);
			
			m_popWnd.closeBtn.addEventListener(MouseEvent.CLICK,OnCloseWnd);
			m_popWnd.panTitle.addEventListener(MouseEvent.MOUSE_DOWN,OnStartDrag);
			m_popWnd.panTitle.addEventListener(MouseEvent.MOUSE_UP,OnStopDrag);
			
//			this.viewSprite.addChild(m_popWnd);
			m_popWnd.addChild(m_dgComponentProperty);
		}
		
		override public function onRemove():void
		{
			m_dgComponentProperty.removeEventListener(XEvent.XEVENT_CHANGED,OnPropertyChanged);
			
			m_dgComponentProperty.removeEventListener(KeyboardEvent.KEY_DOWN,OnKeyDown);
			
			m_popWnd.closeBtn.removeEventListener(MouseEvent.CLICK,OnCloseWnd);
			m_popWnd.panTitle.removeEventListener(MouseEvent.MOUSE_DOWN,OnStartDrag);
			m_popWnd.panTitle.removeEventListener(MouseEvent.MOUSE_UP,OnStopDrag);
			
			m_dgComponentProperty = null;
			m_dpComponentProperty = null;
			m_id = "";
			m_component = null;
			m_popWnd = null;
		}
		
		private function OnCloseWnd(evt : MouseEvent) : void
		{
			XG.closeUI(NAME);
		}
		
		private function OnStartDrag(evt : MouseEvent) : void
		{
			this.viewSprite.startDrag();
		}
		
		private function OnStopDrag(evt : MouseEvent) : void
		{
			this.viewSprite.stopDrag();
		}
		
		public function setComponentProperty(id : String,component:*) : void
		{
			m_dpComponentProperty.removeAll();
			m_dgComponentProperty.rowHeight = 22;
			m_comPropertyLst = [];
			
			m_id = id;
			m_component = component;
			
			var pro:Object = Manager.XML.ComponentProperty.getProperty(id); 
			
			for each(var proInfo : * in pro)
			{
				for each(var name : String in proInfo)
				{
					m_comPropertyLst.push({propertyName:name,propertyValue:component[name]});
				}
			}
			
			m_dgComponentProperty.height = m_comPropertyLst.length*m_dgComponentProperty.rowHeight + m_dgComponentProperty.headerHeight;
			
			m_dpComponentProperty.addItems(m_comPropertyLst);
		}
		
		public function changeComponentProperty() : void
		{
			m_dpComponentProperty.removeAll();
			m_dgComponentProperty.rowHeight = 22;
			m_comPropertyLst = [];
			
			var pro:Object = Manager.XML.ComponentProperty.getProperty(m_id); 
			
			for each(var proInfo : * in pro)
			{
				for each(var name : String in proInfo)
				{
					m_comPropertyLst.push({propertyName:name,propertyValue:m_component[name]});
				}
			}
			
			m_dgComponentProperty.height = m_comPropertyLst.length*m_dgComponentProperty.rowHeight + m_dgComponentProperty.headerHeight;
			
			m_dpComponentProperty.addItems(m_comPropertyLst);
		}
		
		private function OnKeyDown(evt : KeyboardEvent) : void
		{
			if(evt.keyCode == Key.ENTER)
			{
				var cellData : Object = m_dgComponentProperty.selectedItem;
				m_component[cellData["propertyName"]] = cellData["propertyValue"]; 
				sendNotification(COMPONENTPROPERTYCHANGED);
			}
		}
		
		private function OnPropertyChanged(evt : XEvent) : void
		{
//			var info : Object = evt.data;
//			m_component[info["propertyName"]] = info["propertyValue"]; 
		}
	}
}